package com.ocom.webapi.controller;

import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.webapi.service.AgentPageService;
import com.ocom.webapi.vo.agentpage.AddVo;
import com.ocom.webapi.vo.agentpage.ModifyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class AgentPageController {

    @Autowired
    private AgentPageService agentPageService;

    /**
     * 代理商列表（AgentPage/onShow）
     * */
    @GetMapping(CompanyServiceUrl.URL_AgentPageOnShow)
    public Result AgentPageOnShow(Integer agentId,String agentName, Integer page, Integer size){
        if (page == null){
            log.error("page为空");
            return ResultUtil.error(ResultEnum.Err_203,"page为空");
        }
        if (size == null){
            log.error("size为空");
            return ResultUtil.error(ResultEnum.Err_203,"size为空");
        }
        return agentPageService.AgentPageOnShow(agentId,agentName, page,size);
    }


    /**
     * 代理商信息（AgentPage/info）
     * */
    @GetMapping(CompanyServiceUrl.URL_AgentPageInfo)
    public Result AgentPageInfo(Long agentId){
        if (agentId == null){
            log.error("代理商ID为空");
            return ResultUtil.error(ResultEnum.Err_203,"代理商ID为空");
        }
        return agentPageService.AgentPageInfo(agentId);
    }


    /**
     * 代理商修改（AgentPage/modify）
     * */
    @PostMapping(CompanyServiceUrl.URL_AgentPageModify)
    public Result AgentPageModify(@Validated @RequestBody ModifyVo modifyVo){
        if (modifyVo.getAgentId() == null){
            log.error("代理商ID为空");
            return ResultUtil.error(ResultEnum.Err_203,"代理商ID为空");
        }
        if (modifyVo.getAgentName().isEmpty()){
            log.error("代理商修改（AgentPage/modify）代理商名称为空");
            return ResultUtil.error(ResultEnum.Err_203,"代理商修改（AgentPage/modify）代理商名称为空");

        }
        return agentPageService.AgentPageModify(modifyVo);
    }

    /**
     * 代理商新增（AgentPage/add）
     * */
    @PostMapping(CompanyServiceUrl.URL_AgentPageAdd)
    public Result AgentPageAdd(@Validated @RequestBody AddVo addVo){
        return agentPageService.AgentPageAdd(addVo);
    }

}
